<?php
/**
 * Description of Emms_Cron_ExchangeRates
 *
 * @author andres
 */
class Emms_Cron_ExchangeRates extends Emms_Cron implements Emms_Interface_Cron
{
    public function update()
    {
        $this->db->beginTransaction();
        try {
            $currencies = new Zend_Db_Table(array('name' => 'currencies', 'primary' => 'CurrencyId'));
            $reference = $currencies->find($this->conf->param->currency->reference)->current();
            $data = $currencies->fetchAll($currencies->select()
                ->where('CurrencyId != ?', $reference->CurrencyId));
        
            $xrates = new Zend_Db_Table(array('name' => 'exchange_rates', 'primary' => 'ExchangeRateId'));

            $soapClient = new soapclient($this->conf->param->currency->xrate_url);
            foreach($data as $currency) { 
                $result = $soapClient->Convert(array(
                    'CurrencyFrom' => $reference->Code,
                    'CurrencyTo' => $currency->Code,
                    'ValueFrom' => (double)1,
                    'UserKey' => $this->conf->param->currency->xrate_key,
                ));
                $xrates->insert(array(
                    'CurrencyId' => $currency->CurrencyId, 
                    'Rate' => $result->ConvertResult, 
                    'Date' => $this->date));
            }    
            $xrates->insert(array(
                'CurrencyId' => $reference->CurrencyId, 
                'Rate' => 1, 
                'Date' => $this->date));
        } catch (Exception $e) {
            
            $this->db->rollBack();
            throw $e;
        }
        $this->db->commit();
    }
}
    
    

